package com.project.school_flate.serviceimpl.reportForms;

import com.mybatisflex.core.query.QueryWrapper;
import com.project.school_flate.dto.shop.ShopInfoDto;
import com.project.school_flate.entity.order.OrderTakeaway;
import com.project.school_flate.entity.shop.ShopInfo;
import com.project.school_flate.entity.table.order.OrderTakeawayTable;
import com.project.school_flate.mapper.order.OrderTakeawayMapper;
import com.project.school_flate.mapper.shop.ShopInfoMapper;
import com.project.school_flate.service.reportForms.ReportFormsShopService;
import com.project.school_flate.util.DateUtils;
import com.project.school_flate.util.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
public class ReportFormsShopServiceImpl implements ReportFormsShopService {

    @Autowired
    private OrderTakeawayMapper oOrderTakeawayMapper;

    @Autowired
    private ShopInfoMapper oShopInfoMapper;

    /**
     * 获取累计单量
     * @param oShopInfoDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getAllRobOrder(ShopInfoDto oShopInfoDto) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(OrderTakeawayTable.ORDER_TAKEAWAY.SHOP_ID.eq(oShopInfoDto.getId()));
        queryWrapper.where(OrderTakeawayTable.ORDER_TAKEAWAY.SHOP_STATE.eq(2));
        List<OrderTakeaway> oOrderTakeawayList = oOrderTakeawayMapper.selectListByQuery(queryWrapper);
        return Result.ok(oOrderTakeawayList.size());
    }

    /**
     * 获取今日抢单
     * @param oShopInfoDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getDayRobOrder(ShopInfoDto oShopInfoDto) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(OrderTakeawayTable.ORDER_TAKEAWAY.SHOP_ID.eq(oShopInfoDto.getId()));
        queryWrapper.where(OrderTakeawayTable.ORDER_TAKEAWAY.SHOP_STATE.eq(2));
        //今天开始
        String createTimeBegin = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00") .withZone(ZoneId.systemDefault()).format(Instant.now().minus(0, ChronoUnit.DAYS));
        //今天结束
        String createTimeEnd = DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59") .withZone(ZoneId.systemDefault()).format(Instant.now().minus(0, ChronoUnit.DAYS));
        queryWrapper.where(OrderTakeawayTable.ORDER_TAKEAWAY.CREATE_TIME.between(DateUtils.stringTurnDate(createTimeBegin,"yyyy-MM-dd HH:mm:ss"),DateUtils.stringTurnDate(createTimeEnd,"yyyy-MM-dd HH:mm:ss")));
        List<OrderTakeaway> oOrderTakeawayList = oOrderTakeawayMapper.selectListByQuery(queryWrapper);
        return Result.ok(oOrderTakeawayList.size());
    }

    /**
     * 获取我的余额
     * @param oShopInfoDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getDayRobOrderBalance(ShopInfoDto oShopInfoDto) throws Exception {
        ShopInfo oShopInfo = oShopInfoMapper.selectOneById(oShopInfoDto.getId());
        return Result.ok(oShopInfo.getBalance());
    }

}
